package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Transactional
    @Override
    public void saveWithDish(SetmealVO setmealVO) {
        //提取数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO, setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        //保存套餐信息
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //插入套餐与菜品关联信息,缺少套餐id
        setmealDishMapper.insertBatch(setmealDishes);


    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //判断是否起售
        List<Setmeal> setmeals=setmealMapper.getByIds(ids);

        for (Setmeal setmeal : setmeals) {
            if(setmeal.getStatus()==StatusConstant.DISABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }


        //批量删除

        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteBySetmealId(ids);
    }

    @Override
    public SetmealVO getById(Long id) {
        //根据id查询setmeal表
        Setmeal setmeal=setmealMapper.getById(id);

        //根据setmealId 查询setmeal_dish表 查询出套餐与菜品id
        List<SetmealDish> setmealDishes=setmealDishMapper.getSetmealDishBySetmealId(id);
        //分装为setmealVo对象
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }
    @Transactional
    @Override
    public void update(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO, setmeal);
        // 更新setmeal表,注意加入注解
        setmealMapper.update(setmeal);
        Long setmealId = setmealVO.getId();
        List<Long> sentmealIds = new ArrayList<>();
        sentmealIds.add(setmealId);
        //处理sentmeal_dish表。 方式就是先删除 在插入
        setmealDishMapper.deleteBySetmealId(sentmealIds);
        //前端传来setmealDishes中没有当前套餐id
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }

        setmealDishMapper.insertBatch(setmealDishes);

    }
    @Transactional
    @Override
    public void startOrStop(Long id, Integer status) {
        if(status==StatusConstant.DISABLE){
            //修改状态为停售
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealMapper.update(setmeal);
        }else{//想要起售
            //根据套餐id 查询菜品id,再根据菜品id 查询菜品
            List<SetmealDish> setmealDishs = setmealDishMapper.getSetmealDishBySetmealId(id);
            List<Long> dishIds = new ArrayList<>();
            for (SetmealDish setmealDish : setmealDishs) {
                dishIds.add(setmealDish.getDishId());
            }
            List<Dish> dishes = new ArrayList<>();
            for (Long dishId : dishIds) {
//                Dish dish = dishMapper.getById(dishId);
//                dishes.add(dish);
            }

            // 检擦菜品状态
            for (Dish dish : dishes) {
                if(dish.getStatus()==StatusConstant.DISABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
            // 进行停售或者起售
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.update(setmeal);
        }
    }
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
